#!/bin/bash

### Container information
#Paths
DOCKER_ROOT="/var/www/nailgun/docker"
IMAGE_DIR="${DOCKER_ROOT}/images"
SOURCE_DIR="${DOCKER_ROOT}/sources"
#SUPERVISOR_CONF_DIR="${docker_root}/supervisor.d"
#SUPERVISOR_CONF_DIR="${docker_root}/supervisor"
#SUPERVISOR_CONF_DIR="<%= @config_dir %>/supervisor/"
ASTUTE_YAML="/etc/fuel/astute.yaml"
DOCKER_ENGINE="native"
ADMIN_IP="10.20.0.2"
LOCAL_IP="127.0.0.1"

#Version of Fuel to deploy
VERSION=$(awk '/release/{gsub(/"/, "");print $2}' /etc/fuel/version.yaml || echo 6.1)
IMAGE_PREFIX="fuel"
# busybox image for storage containers
BUSYBOX_IMAGE="busybox.tar.gz"
# base centos image for all containers
BASE_IMAGE="centos.tar.gz"
# Order in which to start all containers
CONTAINER_SEQUENCE="postgres rabbitmq keystone rsync astute rsyslog nailgun ostf nginx cobbler mcollective"

# storage container names
PREFIX_STORAGE="fuel-core-${VERSION}-volume-"

# app container names
declare -A CONTAINER_NAMES
prefix="fuel-core-${VERSION}-"
CONTAINER_NAMES['cobbler']="${prefix}cobbler"
CONTAINER_NAMES['postgres']="${prefix}postgres"
CONTAINER_NAMES['rabbitmq']="${prefix}rabbitmq"
CONTAINER_NAMES['rsync']="${prefix}rsync"
CONTAINER_NAMES['astute']="${prefix}astute"
CONTAINER_NAMES['nailgun']="${prefix}nailgun"
CONTAINER_NAMES['ostf']="${prefix}ostf"
CONTAINER_NAMES['nginx']="${prefix}nginx"
CONTAINER_NAMES['rsyslog']="${prefix}rsyslog"
CONTAINER_NAMES['mcollective']="${prefix}mcollective"
CONTAINER_NAMES['keystone']="${prefix}keystone"

# app container options
declare -A CONTAINER_OPTIONS
base_opts="-t --net=host"
FOREGROUND="-i"
BACKGROUND="-d"
BIND_ALL=""
BIND_ADMIN="${ADMIN_IP}:"
BIND_LOCAL="${LOCAL_IP}:"
CONTAINER_OPTIONS["cobbler"]="-p ${BIND_ALL}53:53/udp -p ${BIND_ALL}69:69/udp -p ${BIND_ALL}80:80 -p ${BIND_ALL}443:443 --privileged $base_opts"
CONTAINER_OPTIONS["postgres"]="-p ${BIND_ADMIN}5432:5432 -p ${BIND_LOCAL}5432:5432 $base_opts"
CONTAINER_OPTIONS["rabbitmq"]="-p ${BIND_ADMIN}5672:5672 -p ${BIND_LOCAL}5672:5672 -p ${BIND_ADMIN}4369:4369 -p ${BIND_LOCAL}4369:4369 -p ${BIND_ADMIN}15672:15672 -p ${BIND_LOCAL}15672:15672 -p ${BIND_ADMIN}61613:61613 -p ${BIND_LOCAL}61613:61613 $base_opts"
CONTAINER_OPTIONS["rsync"]="-p ${BIND_ADMIN}873:873 -p ${BIND_LOCAL}873:873 $base_opts"
CONTAINER_OPTIONS["astute"]="$base_opts"
CONTAINER_OPTIONS["nailgun"]="--privileged -p ${BIND_ADMIN}8001:8001 -p ${BIND_LOCAL}8001:8001 $base_opts"
CONTAINER_OPTIONS["ostf"]="-p ${BIND_ADMIN}8777:8777 -p ${BIND_LOCAL}8777:8777 $base_opts"
CONTAINER_OPTIONS["nginx"]="-p ${BIND_ALL}8000:8000 -p ${BIND_ALL}8080:8080 $base_opts"
CONTAINER_OPTIONS["rsyslog"]="--privileged -p ${BIND_ADMIN}514:514 -p ${BIND_LOCAL}514:514 -p ${BIND_ADMIN}514:514/udp -p ${BIND_LOCAL}514:514/udp -p ${BIND_ADMIN}25150:25150 -p ${BIND_LOCAL}25150:25150 $base_opts"
CONTAINER_OPTIONS["mcollective"]="--privileged $base_opts"
CONTAINER_OPTIONS["keystone"]="-p ${BIND_ALL}5000:5000 -p ${BIND_ALL}35357:35357 $base_opts"

######
#Dependency of volumes for deployment
#####

#Host volumes
declare -A HOST_VOL
HOST_VOL['repo']="-v /var/www/nailgun:/var/www/nailgun:rw -v /etc/yum.repos.d:/etc/yum.repos.d:rw"
HOST_VOL['puppet']="-v /etc/puppet:/etc/puppet:ro"
HOST_VOL['sshkey']="-v /root/.ssh:/root/.ssh:ro"
HOST_VOL['astuteyaml']="-v /etc/fuel:/etc/fuel:ro"
HOST_VOL['logs']="-v /var/log/docker-logs:/var/log"
HOST_VOL['keys']="-v /var/lib/fuel/keys:/var/lib/fuel/keys:rw"
HOST_VOL['ibp']="-v /var/lib/fuel/ibp:/var/lib/fuel/ibp:rw"

#Persistent directories for container data
HOST_VOL['astute_data']="-v /var/lib/fuel/container_data/$VERSION/astute:/var/lib/astute"
HOST_VOL['cobbler_data']="-v /var/lib/fuel/container_data/$VERSION/cobbler:/var/lib/cobbler"
HOST_VOL['postgres_data']="-v /var/lib/fuel/container_data/$VERSION/postgres:/var/lib/pgsql"

#Storage container volume mounts
declare -A CONTAINER_VOLUMES
#Format: CONTAINER_VOLUMES[$FOO_CNT]="${HOST_VOL['foo']}"

#Storage container volumes
#Format: FOO_VOL="--volumes-from $FOO_CNT"

#Nailgun static files are in nailgun container
NAILGUN_VOL="--volumes-from ${CONTAINER_NAMES['nailgun']}"
COBBLER_VOL="--volumes-from ${CONTAINER_NAMES['cobbler']}"
ASTUTE_VOL="--volumes-from ${CONTAINER_NAMES['astute']}"

#App container volume mounts
###requirements cheat sheet
###ALL: astuteyaml repo logs
###puppet: rsync
###sshkey: nailgun cobbler astute mcollective ostf
###repo: nginx mcollective
###nailgun: nginx
###cobbler: mcollective
###astute: mcollective
declare -A REQS
REQS["${HOST_VOL['astuteyaml']}"]="cobbler postgres rabbitmq rsync astute nailgun ostf nginx rsyslog mcollective keystone"
REQS["${HOST_VOL['keys']}"]="mcollective astute"
REQS["${HOST_VOL['repo']}"]="cobbler postgres rabbitmq rsync astute nailgun ostf nginx rsyslog mcollective keystone"
REQS["${HOST_VOL['logs']}"]="cobbler postgres rabbitmq rsync astute nailgun ostf nginx rsyslog mcollective keystone"
#Most containers work from local integrated puppet, but rsync needs to serve host puppet
REQS["${HOST_VOL['puppet']}"]="rsync"
#Uncomment to enable host puppet manifests
#REQS["${HOST_VOL['puppet']}"]="cobbler postgres rabbitmq rsync astute nailgun ostf nginx rsyslog mcollective keystone"
REQS["${HOST_VOL['astute_data']}"]="astute"
REQS["${HOST_VOL['cobbler_data']}"]="cobbler"
REQS["${HOST_VOL['postgres_data']}"]="postgres"
REQS["${HOST_VOL['sshkey']}"]="nailgun cobbler astute mcollective ostf"
REQS["${HOST_VOL['ibp']}"]="mcollective"
REQS["$NAILGUN_VOL"]="nginx"
REQS["$COBBLER_VOL"]="mcollective"
REQS["$ASTUTE_VOL"]="mcollective"

#assemble requirements
for requirement in "${!REQS[@]}"; do
  for container in ${REQS["$requirement"]}; do
    CONTAINER_VOLUMES["$container"]+="${requirement} "
  done
done

#backup settings
SYSTEM_DIRS="/etc/fuel /var/lib/fuel /root/.ssh"
FULL_BACKUP_DIRS="/etc/puppet /var/www/nailgun"
BACKUP_ROOT="/var/backup/fuel"

# number of retries for "docker check"
CHECK_RETRIES=240
